// miniprogram/pages/order_list/detail.js
import req from "../../../utils/request";
import {
  USER_GET_ORDER_DETAIL_BYID,
  ORDER_UPDSTE_POST_CANCEL,
  TEST_PAY,
  POST_ORDER_RECEIVE,
} from "../../../messages/uris";
import { toast } from "../../../utils/toast";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userOrderId: null,
    userOrder: {},
    userOrderStatus: null,
    //弹出层
    topTips: false,
    hide: false,
    tipValue: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userOrderId = options.userOrderId;
    this.data.userOrderId = userOrderId;

    //根据订单明细ID查询订单信息
    const that = this;
    req.get(
      USER_GET_ORDER_DETAIL_BYID + "?id=" + userOrderId,
      {},
      (resp) => {
        that.setData({
          userOrder: resp.data.result,
          userOrderStatus: resp.data.result.orderStatus,
        });
      },
      (resp) => {
        console.log("查询订单详情失败");
      },
      (res) => {},
      that
    );
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  //取消订单的弹窗
  showUpdateMode: function (event) {
    const that = this;
    wx.showModal({
      title: "提示",
      content: "是否取消订单？",
      success: function (res) {
        if (res.confirm) {
          //调用取消订单的方法
          if (event.currentTarget.dataset.id) {
            that.updateState(event.currentTarget.dataset.id);
          }
        }
      },
    });
  },

  //102 取消订单
  updateState: function (userorderid) {
    const that = this;
    wx.showLoading({
      title: "请求中...",
    });
    req.get(
      ORDER_UPDSTE_POST_CANCEL + "?userOrderId=" + userorderid,
      {},
      (resp) => {
        wx.showToast({
          title: "取消订单成功",
          icon: "", //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
          duration: 2000, //停留时间
        });
        //重新设置 当前订单的状态 避免重新刷新页面去查询
        that.setData({
          userOrderStatus: 106,
        });
        wx.hideLoading();
      },
      (resp) => {
        wx.showToast({
          title: "取消订单失败",
          icon: "", //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
          duration: 2000, //停留时间
        });
        wx.hideLoading();
      },
      (res) => {},
      that
    );
  },

  orderPay: function () {
    const that = this;
    // toast("模拟支付 !!!");
    wx.showModal({
      title: "模拟支付提示",
      content: "当前支付未开通, 模拟支付行为, 但为进行任何支付相关行为",
      success: function (res) {
        if (res.confirm) {
          req.post(
            TEST_PAY,
            { orderId: that.data.userOrderId },
            (res) => {
              toast("模拟支付成功");
            },
            (res) => {},
            (res) => {},
            that
          );
        } else {
          // toast("模拟支付失败", "error");
        }
      },
      fail: function (res) {
        // toast("模拟支付失败", "error");
      },
    });
  },

  //105 确认收货
  confirmReceipt:function(){
    const that = this;
    // toast("模拟支付 !!!");
    wx.showModal({
      title: "模拟支付提示",
      content: "当前支付未开通, 模拟支付行为, 但为进行任何支付相关行为",
      success: function (res) {
        if (res.confirm) {
          req.post(
            TEST_PAY,
            { orderId: that.data.userOrderId },
            (res) => {
              toast("确认收货成功");
            },
            (res) => {},
            (res) => {},
            that
          );
        } else {
          // toast("模拟支付失败", "error");
        }
      },
      fail: function (res) {
        // toast("模拟支付失败", "error");
      },
    });
  },

  //骑手确认订单窗口
  confirmAnOrderModel:function(event){
    const that = this;
    wx.showModal({
      title: "提示",
      content: "是否确认订单？",
      success: function (res) {
        if (res.confirm) {
          //确认接单方法
          if (event.currentTarget.dataset.id) {
            that.confirmAnOrder(event.currentTarget.dataset.id);
          }
        }
      },
    });
  },

  //骑手确认接单
  confirmAnOrder: function (userOrderId) {
    console.log(userOrderId)
    const that = this;
    wx.showLoading({
      title: "请求中...",
    });
    req.get(
      POST_ORDER_RECEIVE + "/" + userOrderId,
      {},
      (resp) => {
        wx.showToast({
          title: "接单成功",
          icon: "", //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
          duration: 2000, //停留时间
        });
        //重新设置 当前订单的状态 避免重新刷新页面去查询
        that.setData({
          userOrderStatus: 103,
        });
        wx.hideLoading();
        wx.redirectTo({
          url: '../order_list'
        })
      },
      (resp) => {
        wx.showToast({
          title: "该订单已经被接走",
          icon: "", //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
          duration: 2000, //停留时间
        });
        wx.hideLoading();
      },
      (res) => {},
      that
    );
  },

});
